package com.fancy.projecty.B_流程实例;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 全部流程实例挂起与激活
 * 会影响 ACT_RE_PROCDEF 和 ACT_RU_EXECUTION 和 ACT_RU_TASK 三张表
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class I_全部流程实例的挂起与激活 {


	@Autowired
	/*执行管理、包括启动、推进、删除流程实例等操作*/
	private RuntimeService runtimeService;
	@Autowired
	/*任务管理,可以做任务的查询的*/
	private TaskService taskService;
	/*历史记录的查询*/
	@Autowired
	private HistoryService historyService;




	@Autowired
	/*管理流程定义,部署相关的Service*/
	private RepositoryService repositoryService;


	@Test
	public void c_全部流程实例的挂起与激活() {
		//3.查询流程定义的对象
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
				.processDefinitionKey("holiday").singleResult();

		//4.得到当前流程定义的实例是否都为暂停状态
		boolean suspended = processDefinition.isSuspended();

		String processDefinitionId = processDefinition.getId();
		//5.判断
		if (suspended) {
			//说明是暂停，就可以激活操作
			/*
			 可以根据ID的方法,也可以根据key的方法来进行暂停.
			 建议直接用key进行暂停,
			 */
			/**
			 * 参数1:当前流程的id
			 * 参数2:是否激活,true 就是激活
			 * 参数3:当前时间点,如果没有就可以给个null值.
			 */

			repositoryService.activateProcessDefinitionById(processDefinitionId, true
					, null);
			System.out.println("流程定义：" + processDefinitionId + "激活");
		} else {
			//挂起操作
			/**参数1: 当前流程的id
			 * 参数2:是否挂起, true就是挂起
			 * 参数3:当前时间点,如果没有就给个null值.
			 */

			repositoryService.suspendProcessDefinitionById(processDefinitionId, true, null);
			System.out.println("流程定义：" + processDefinitionId + "挂起");
		}

	}
 /*影响了这些表*/
		/*
		* ==>  Preparing: update ACT_RE_PROCDEF set REV_ = ?, SUSPENSION_STATE_ = ?, CATEGORY_ = ? where ID_ = ? and REV_ = ?
==> Parameters: 2(Integer), 2(Integer), http://www.activiti.org/testm1577761336934(String), holiday:1:2504(String), 1(Integer)
<==    Updates: 1
==>  Preparing: update ACT_RU_EXECUTION set REV_ = ?, BUSINESS_KEY_ = ?, PROC_DEF_ID_ = ?, ACT_ID_ = ?, IS_ACTIVE_ = ?, IS_CONCURRENT_ = ?, IS_SCOPE_ = ?, IS_EVENT_SCOPE_ = ?, PARENT_ID_ = ?, SUPER_EXEC_ = ?, SUSPENSION_STATE_ = ?, CACHED_ENT_STATE_ = ?, NAME_ = ? where ID_ = ? and REV_ = ?
==> Parameters: 2(Integer), 1001(String), holiday:1:2504(String), _3(String), true(Boolean), false(Boolean), true(Boolean), false(Boolean), null, null, 2(Integer), 2(Integer), null, 5001(String), 1(Integer)
<==    Updates: 1
==>  Preparing: update ACT_RU_TASK SET REV_ = ?, NAME_ = ?, PARENT_TASK_ID_ = ?, PRIORITY_ = ?, CREATE_TIME_ = ?, OWNER_ = ?, ASSIGNEE_ = ?, DELEGATION_ = ?, EXECUTION_ID_ = ?, PROC_DEF_ID_ = ?, DESCRIPTION_ = ?, DUE_DATE_ = ?, CATEGORY_ = ?, SUSPENSION_STATE_ = ?, FORM_KEY_ = ? where ID_= ? and REV_ = ?
==> Parameters: 2(Integer), 填写请假申请单(String), null, 50(Integer), 2020-01-02 10:13:33.531(Timestamp), null, zhangsan(String), null, 5001(String), holiday:1:2504(String), null, null, null, 2(Integer), null, 5004(String), 1(Integer)
<==    Updates: 1
		*
		* */
}
